<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>2_不用柯里化的实现</title>
    <!-- 引入react核心库 -->
    <script src="../React-js/react.development.js"></script>
    <!-- 引入react-dom,用于支持react操作DOM -->
    <script
      type="text/javascript"
      src="../React-js/react-dom.development.js"
    ></script>
    <!-- 引入babel,用于将jsx转换为js -->
    <script type="text/javascript" src="../React-js/babel.min.js"></script>
  </head>
  <body>
    <div id="test"></div>
    <!-- 维护状态 -->
    <script type="text/babel">
      // 1.创建组件
      class Login extends React.Component {
        // 初始化状态
        state = { username: null, password: null };

        // 保存表单数据到状态中
        handleSaveData = (dataType, e) => {
          this.setState({ [dataType]: e.target.value });
        };

        // 处理提交 表单的回调
        handleSubmit = (event) => {
          event.preventDefault(); //阻止表单的提交
          const { username, password } = this.state;
          alert(`用户名${username},密码：${password}`);
        };

        render() {
          return (
            <form action="http://www.baidu.com" onSubmit={this.handleSubmit}>
              <label htmlFor="username">用户名</label>
              <input
                onChange={(e) => {
                  this.handleSaveData('username', e);
                }}
                type="text"
                id="username"
                name="username"
              />
              <label htmlFor="password">密码</label>
              <input
                onChange={(e) => {
                  this.handleSaveData('password', e);
                }}
                type="password"
                id="password"
                name="password"
              />
              <button>登录</button>
            </form>
          );
        }
      }
      // 2.渲染
      ReactDOM.render(<Login />, document.getElementById('test'));
    </script>
  </body>
</html>
